package com.tanklab.signature.common.util.sm2Util;



import org.bouncycastle.util.encoders.Hex;

import java.security.PublicKey;
import java.util.Base64;

/**
 * 加解密方法包装类
 */
public class Sm2 {

    //对输入信息进行加密，得到加密后对象
//    public static securityStore Encrypt(String str) {
//        PublicKey publicKey = null;
//        PrivateKey privateKey = null;
//        //生成公私钥对
//        try{
//            Map<String,String> keys = KeyUtils.generateSmKey();
//            publicKey = KeyUtils.createPublicKey(keys.get(KeyUtils.PUBLIC_KEY));
//            privateKey = KeyUtils.createPrivateKey(keys.get(KeyUtils.PRIVATE_KEY));
//
//            //公钥加密
//            byte[] encrypt = Sm2Util.encrypt(str.getBytes(), publicKey);
//            //加密转base64
//            String encryptBase64Str = Base64.getEncoder().encodeToString(encrypt);
//
//            //签名
//            byte[] sign = Sm2Util.signByPrivateKey(str.getBytes(), privateKey);
//
//            //公钥验签，验证通过后才能数据解密
//            boolean b = Sm2Util.verifyByPublicKey(str.getBytes(), publicKey, sign);
//            if(b==false){
//                System.out.println("Can not encrypt");
//                return null;
//            }
//            securityStore store = new securityStore(keys.get(KeyUtils.PUBLIC_KEY),keys.get(KeyUtils.PRIVATE_KEY),encryptBase64Str,null,b);
//            return store;
//        }
//        catch(Exception e){
//            e.printStackTrace();
//            return null;
//        }
//    }

    public static String Encrypt(String str,String publicKey1) throws Exception {
            PublicKey publicKey = KeyUtils.createPublicKeyFromBase(publicKey1);
            //公钥加密

//            PrivateKey privateKey1 = KeyUtils.createPrivateKey(privateKey);
//            byte[] sign = Sm2Util.signByPrivateKey(str.getBytes(), privateKey1);
//            boolean b = Sm2Util.verifyByPublicKey(str.getBytes(), publicKey, sign);
//            if(b==false){
//                System.out.println("Can not encrypt");
//                return null;
//            }
        //验签，是否能解密
            byte[] encrypt = Sm2Util.encrypt(str.getBytes(), publicKey);
            //加密转base64
            String encryptBase64Str = Base64.getEncoder().encodeToString(encrypt);

            return encryptBase64Str;

    }//纯后端加密
    public static String Decrypt(String encrpt,String privateKey) throws Exception{
        try {
            encrpt = "04" + encrpt;
            byte[] decode = Hex.decode(encrpt);
//            byte[] decode = Base64.getDecoder().decode(encrpt);
            byte[] decrypt = Sm2Util.decrypt(decode, KeyUtils.createPrivateKey(privateKey));
            assert decrypt != null;
            return new String(decrypt);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String DecryptFromBase(String encrpt,String privateKey) throws Exception{
        try {//纯后端解密
//            encrpt = "04" + encrpt;
//            byte[] decode = Hex.decode(encrpt);
            byte[] decode = Base64.getDecoder().decode(encrpt);
            byte[] decrypt = Sm2Util.decryptFromBase(decode, KeyUtils.createPrivateKey(privateKey));
            assert decrypt != null;

            return new String(decrypt);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
